﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonModule;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using ePlanet.ArchiveMange.Models;
using System.Data;
using System.Data.Common;

namespace ePlanet.ArchiveMange.DataAccess
{
    public class ZongheVolFileDAL:DataHelper
    {
        public ZongheVolFileDAL()
        {
            if (!(this.DBHelper.database is OracleDatabase))
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        public ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable GetAllZongheVolFileData()
        {
            string sql = @"select *
from OLISEARCH_ZONGHE_VOL_FILE t";
            ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable data = new ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ds.Tables[0].TableName = data.TableName;
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }


        /// <summary>
        /// 根据主键查询数据
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable GetAllZongheVolFileDataById(string DATAID)
        {
            string sql = @"select *
from OLISEARCH_ZONGHE_VOL_FILE t where DATAID='" + DATAID + "'";
            ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable data = new ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }


        /// <summary>
        /// 根据查询条件查询勘探综合卷内件信息
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable GetAllDocumenArchivetByFilter(string filter)
        {

            string sql = @"select *
from OLISEARCH_ZONGHE_VOL_FILE t  ";
            if (!string.IsNullOrEmpty(filter))
            {
                sql += "where 1=1 and " + filter;
            }
            ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable data = new ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 备注：根据DATAID列表取对应档案的详细信息
        /// 时间：2013年1月9日 23:47:41
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public DataTable GetZongheVolFileDataByIds(IEnumerable<string> idList)
        {
            if (idList == null || idList.Count() == 0)
            {
                return null;
            }
            var ids = string.Join(",", (from id in idList select string.Format("'{0}'", id)).ToArray());
            string sql = string.Format(@"select a.createuserid,
       a.dataid, 
       a.statusid,
       a.fullfondsnum,
       a.archivenum,
       '' archivesource,
       a.title,
       a.pagecou pagenum,--页数
       null filenum,  --文件编号
       0 pagefilenum, --件数
       to_char(a.createtime ,'yyyy-mm-dd') createtime,
       a.createtime,
       '' publisher,
       a.fileorgid,
       null mainbodynum,--正本数量
       1 fs,
       '件' remark,
       b.code_desc as f_keeptime,
       c.code_desc as f_securitytype
  from OLISEARCH_ZONGHE_VOL_FILE a
  left join p_bt_codetable b
    on a.f_keeptime = b.codetableid
  left join p_bt_codetable c
  on a.f_securitytype = c.codetableid
  where a.DATAID  in ({0})", ids);
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        public DataTable GetHandoverDataByIds(IEnumerable<string> idList)
        {
            throw new Exception("未实现");
//            if (idList == null || idList.Count() == 0)
//            {
//                return null;
//            }
//            var ids = string.Join(",", (from id in idList select string.Format("'{0}'", id)).ToArray());
//            string sql = string.Format(@"SELECT A.DATAID,
//       A.ARCHIVESOURCE,
//       A.CREATETIME,
//       A.FILEORGID,
//       A.MAINBODYNUM, --正本数量
//       A.doplicatebodynum,--副本数量
//       B.CODE_DESC     AS F_CARRIERTYPE, --载体类型
//       C.CODE_DESC     AS F_KEEPTIME       --保管期限
//  FROM olisearch_archive A
//  LEFT JOIN P_BT_CODETABLE B
//    ON A.F_CARRIERTYPE = B.CODETABLEID
//  LEFT JOIN P_BT_CODETABLE C
//    ON A.F_KEEPTIME = C.CODETABLEID
//  where a.DATAID  in ({0})", ids);
//            if (this.DBHelper.database is OracleDatabase)
//            {
//                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
//                if (ds != null && ds.Tables.Count > 0)
//                {
//                    return ds.Tables[0];
//                }
//                return null;
//            }
//            else
//            {
//                throw new Exception("只支持Oracle数据库");
//            }
        }

        public DataTable GetZongheVolFileDateSpanByIds(IEnumerable<string> idList)
        {
            if (idList == null || idList.Count() == 0)
            {
                return null;
            }
            var ids = string.Join(",", (from id in idList select string.Format("'{0}'", id)).ToArray());
            string sql = string.Format(@"SELECT   MAX(a.createtime) endtime , MIN(a.createtime) starttime
FROM OLISEARCH_ZONGHE_VOL_FILE a 
WHERE a.dataid IN ({0})", ids);
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 根据档案类型查询数据
        /// </summary>
        /// <param name="typeId"></param>
        /// <returns></returns>
        public ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable GetZongheVolFileDataByType(string archiveType)
        {
            string sql = @"select *
from OLISEARCH_ZONGHE_VOL_FILE t where archiveType='" + archiveType + "'";
            ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable data = new ArchiveMangeModal.OLISEARCH_ZONGHE_VOL_FILEDataTable();
            if (this.DBHelper.database is OracleDatabase)
            {
                DataSet ds = this.DBHelper.ExecuteDataSet(sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    data.Merge(ds.Tables[0]);
                }
                return data;
            }
            else
            {
                throw new Exception("只支持Oracle数据库");
            }
        }

        /// <summary>
        /// 删除数据(历史数据不能删除)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DeleteData(string key)
        {
            string sql = "delete from OLISEARCH_ZONGHE_VOL_FILE where DATAID=:DATAID";
            DbCommand cmd = this.DBHelper.database.GetSqlStringCommand(sql);
            this.DBHelper.database.AddParameter(cmd, "DATAID", DbType.String, ParameterDirection.Input, "DATAID", DataRowVersion.Current, key);
            int rowcount = this.DBHelper.database.ExecuteNonQuery(cmd);
            return rowcount > 0 ? true : false;
        }

        public bool IsSameSecret(List<Guid> dataIDList)
        {
            return true;
        }
    }
}
